<?php

/**
 * 模型对应数据库表 "product_base".
 *
 * 以下是可用表product_base的栏位:
 * @property integer $id
 * @property string $p_name
 * @property string $p_price
 * @property integer $p_quntity
 * @property double $p_rebate
 * @property integer $p_type
 * @property integer $p_game
 * @property string $p_note
 * @property string $p_img
 */
class ProductBase extends CActiveRecord {

    public $p_pro_array = array(
        '1' => '--产品属性--',
        '2' => '推荐',
        '3' => '特价',
        '4' => '热卖'
    );

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return ProductBase the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'product_base';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('p_name, p_price, p_game, p_type', 'required'),
            array('p_quntity, p_game', 'numerical', 'integerOnly' => true),
            array('p_rebate', 'numerical'),
            array('p_name', 'length', 'max' => 150),
            array('p_price', 'numerical'),
            array('p_img', 'length', 'max' => 225),
            array('p_note, zam_number', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, p_name, p_price, p_quntity, p_rebate, p_type, p_game, p_note, p_img', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'p_game' => '游戏',
            'p_type' => '产品类型',
            'p_name' => '产品名称',
            'p_price' => '产品价格($)',
            'p_quntity' => '产品数量',
            'p_rebate' => '折扣',
            'p_pro' => '产品属性',
            'p_note' => '产品说明',
            'p_img' => '产品图片',
            'p_time' => '最后修改时间',
            'zam_number' => '第三方ID'
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('p_name', $this->p_name, true);
        $criteria->compare('p_price', $this->p_price, true);
        $criteria->compare('p_type', $this->p_type);
        $criteria->compare('p_game', $this->p_game);
        $pageSize = 30;
        if ($this->p_game && $this->p_type)
            $pageSize = 10000;


        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'p_game ASC, p_name ASC',
                    ),
                    'pagination' => array(
                        'pageSize' => $pageSize
                    ),
                ));
    }

    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        //$cTag = $name=='p_price'?"<span style='float:left'>$ </span>":''; 
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
    }

    /**
     * Get product by type and game id
     * @param type $game_id
     * @param type $type
     * @return type
     */
    public function getProductsByType() {
        $cr = new CDbCriteria;
        $cr->compare('p_name', $this->p_name, TRUE);
        $cr->compare('p_game', $this->p_game);
        $cr->compare('p_type', $this->p_type);
        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $cr,
                    'sort' => array(
                        'defaultOrder' => 'p_price ASC',
                    ),
                    'pagination' => array(
                        'pageSize' => 10
                    ),
                ));
    }

    /**
      +-----------------------------------------------------
     * 说明 获取产品价格
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getProductPrice($pModel) {
        $rebate = $pModel->p_rebate;//折扣
        $pPrice = $pModel->p_price;
        $price = $rebate * $pPrice;
        $price = WebPrice::model()->getFunctionPrice($price, $pModel->p_game, '', $pModel->p_type);
        $aPrice = Yii::app()->getController()->changeCurrency($price);
        $aSave = Yii::app()->getController()->changeCurrency($pPrice - $price);
        return $aPrice + array('save' => $aSave['price']);
    }

    /**
     * 生成购买按钮
     */
    public function createBuyButton() {
        $num = 1;
        if($this->p_type == 'ITEM'){
            $nums = $this->getNums();
            $num = $nums['num'][0];
        }
        return '<form style="margin:0;">
                <input type="hidden" name="game" value="' . $this->p_game . '" />
                <input type="hidden" name="server" value="0" />
                <input type="hidden" name="product_type" value="' . $this->p_type . '" />
                <input type="hidden" name="product" value="' . $this->id . '" />
                <input type="hidden" name="num" value="' . $num . '" />
                <input class="add-to-cart getAttr btn" rel="add" onclick="" type="button" value="Buy Now" />
                </form>';
    }

    /**
     * 获取产品数量级内容
     * @return null or array
     */
    public function getNums() {
        $game = ProductCategory::model()->findByPk($this->p_game);
        //产品属性游戏ID
        $game_id = $game->parentid ? $game->parentid : $this->p_game;
        $attr = ProductAttribute::model()->findByAttributes(array(
            'game_id' => $game_id,
            'product_type' => $this->p_type,
            'input_type' => 'NUMS',
                ));

        if ($attr) {
            $attrValue = ProductAttributeValue::model()->findByAttributes(array(
                'product_id' => $this->id,
                'attribute_id' => $attr->id,
                    ));
            if ($attrValue) {
                $value = unserialize($attrValue->attribute_value);
                return $value;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
    
    /**
     * 生成材料数据下拉框
     * @return type
     */
    public function itemNumDropDownList(){
        $nums_value = $this->getNums();
        
        //$drop = CHtml::dropDownList('quantity', '', array_combine($nums_value['num'], $nums_value['num']), array('class' => 'span1', 'onchange' => 'computePrice(this)'));
        $un_price = CHtml::hiddenField('un_price', $this->p_price);
        $drop = '<select class="span1" onchange="computePrice(this)">';
        foreach ($nums_value['num'] as $key=>$v){
            $drop .= '<option value="'.$v.':'. $nums_value['discount'][$key].'">'.$v.'</option>';
        }
        $drop .= '</select>';
        return $drop.$un_price;
        
    }
    
    /**
     * 材料初始价格
     * @return type
     */
    public function itemPrice(){
        $nums = $this->getNums();
        $discount = $nums['discount'][0] > 1 ? $nums['discount'][0]/100 : $nums['discount'][0];
        $price = $this->p_price * (float)$nums['num'][0] * (float)$discount;
        $pp = Yii::app()->getController()->changeCurrency($price);
        return round($pp['price'], 2);
    }
    
    /**
     * 普通产品价格
     * @return float
     */
    public function defaultProductPrice(){
        $price = Yii::app()->getController()->changeCurrency($this->p_price);
        return round($price['price'], 2);
    }
    
    /**
     * 当然货币符号
     * @return type
     */
    public function getCurrencyTag(){
        $currency = Yii::app()->getController()->changeCurrency(0);
        return $currency['tag'];
    }

}
